package com.example.javafx.选择框;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

import java.awt.*;

/**
 * 选择框
 */
public class Main extends Application {

    Rectangle rect = new Rectangle(150,30);

    final Label label = new Label("Hello");

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(new Group());
        scene.setFill(Color.ALICEBLUE);
        primaryStage.setScene(scene);
        primaryStage.show();

        primaryStage.setWidth(300);
        primaryStage.setHeight(200);

        label.setStyle("-fx-font: 25 arial;");
        label.setLayoutX(40);

        rect.setStroke(Color.BLUE);
        rect.setStrokeWidth(3);
        rect.setFill(Color.WHITE);

        final String[] greetings = new String[]{"A","B","C","D","E"};

        final ChoiceBox<String> cb = new ChoiceBox<>(
                FXCollections.observableArrayList("a","b","c","d","e"));

        cb.getSelectionModel().selectedIndexProperty().addListener(
                (observable, oldValue, newValue) -> {
                    label.setText(greetings[newValue.intValue()]);
                }
        );

        cb.setTooltip(new Tooltip("Select the language"));
        cb.setValue("English");
        HBox hBox = new HBox();
        hBox.getChildren().addAll(cb,label);
        hBox.setSpacing(30);
        hBox.setAlignment(Pos.CENTER);
        hBox.setPadding(new Insets(10,0,0,10));
        ((Group)scene.getRoot()).getChildren().add(hBox);

    }

    public static void main(String[] args) {
        launch(args);
    }
}
